package com.atom.app.mvi.ui.rd

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.atom.app.mvi.R
import com.atom.module.logger.Logger
import com.atom.module.mvi.android.lifecycle.createDestroy
import com.atom.module.mvi.android.view.MVIActivity
import com.atom.module.mvi.binder.Binder

sealed class LaunchUIEvent {

}

data class LaunchViewModel(val aa: String)

sealed class LaunchRouter(val router: String) {

    companion object {
        fun getAll() = mutableListOf(
            Chat,
            Document,
            AppCenter,
            Mine,
            More,
            Project,
            Work,
            WillDo,
        )
    }

    @Composable
    abstract fun ConsumeLayout(launch: LaunchActivity)

    // 聊天界面
    object Chat : LaunchRouter("Chat") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 文档
    object Document : LaunchRouter("Document") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 应用
    object AppCenter : LaunchRouter("AppCenter") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 我的
    object Mine : LaunchRouter("Mine") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 更多
    object More : LaunchRouter("More") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 项目
    object Project : LaunchRouter("Project") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 工作
    object Work : LaunchRouter("Work") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }

    // 待办
    object WillDo : LaunchRouter("WillDo") {
        @Composable
        override fun ConsumeLayout(launch: LaunchActivity) {

        }
    }
}

class LaunchActivity : MVIActivity<LaunchUIEvent, LaunchViewModel>() {

    override fun binder(): Binder {
        return this.lifecycle.createDestroy {

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { LaunchContentView() }
    }

    override fun accept(t: LaunchViewModel) {

    }

    @Preview
    @Composable
    private fun LaunchContentView() {
        // 使用导航 , 相当于 fragment的
        val routers = LaunchRouter.getAll()
        // 界面控制器
        val navController = rememberNavController()

        Surface(modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .height(50.dp)
                    .background(color = Color.Unspecified)
            ) {
                TopAppBar(title = {
                    Text("主页")
                }, navigationIcon = {
                    IconButton(onClick = {
                        Logger.e("打印当前的", navController.currentDestination)
                    }) {
                        Icon(Icons.Filled.ArrowBack, null)
                    }
                })
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .weight(1f)
                ) {
                    NavHost(
                        navController = navController, startDestination = LaunchRouter.Chat.router
                    ) {
                        routers.forEach { router ->
                            composable(router.router) {
                                Box(modifier = Modifier.fillMaxSize()) {
                                    Text(text = router.router, fontSize = 50.sp)
                                }
                            }
                        }
                    }
                }

                BottomNavigation {
                    for (index in 0..5) {
                        val item = routers[index]
                        BottomNavigationItem(icon = {
                            Icon(
                                Icons.Filled.Favorite,
                                contentDescription = null,
                                modifier = Modifier
                                    .height(10.dp)
                                    .width(10.dp)
                            )
                        },
                            label = { Text(item.router) },
                            selected = navController.currentDestination?.route.equals(item.router),
                            onClick = { navController.navigate(item.router) })
                    }
                }


            }
        }
    }


    @Preview(name = "IconView")
    @Composable
    fun testIconView() {
        Surface {
            IconView(
                modifier = Modifier
                    .size(60.dp)
                    .clickable {

                    }, icon = painterResource(id = R.mipmap.icon_launcher), title = "biaoti"
            )
        }
    }

    @Composable
    fun IconView(
        modifier: Modifier = Modifier,
        icon: Painter,
        title: String,
        iconContentDescription: String = "",
        weight: Float = 0.618f
    ) {
        Column(
            modifier = modifier, verticalArrangement = Arrangement.Center
        ) {
            Icon(
                painter = icon,
                contentDescription = iconContentDescription,
                tint = Color.Unspecified,
                modifier = Modifier
                    .weight(weight)
                    .fillMaxWidth()
            )
            Text(
                text = title,
                modifier = Modifier
                    .weight(1 - weight)
                    .fillMaxWidth()
                    .wrapContentSize(align = Alignment.Center),
                maxLines = 1
            )
        }
    }
}